package com.yanqu.road.server.cmd.group.rank;

import com.yanqu.road.logic.cross.CrossGroupRankListModel;
import com.yanqu.road.logic.pb.RankPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.rank.CrossRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;

import java.util.List;
import java.util.Map;

@Cmd(code = CrossProtocol.C_BIG_RANK_TYPE_TOP, desc = "根据类型获取分组榜首")
public class GetCrossGroupRankTopByTypeCmd extends CrossCommand {
    private long userId;
    private int groupId;
    private List<Integer> typeList;
    private int bigType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankProto.CrossGroupRankTopByTypeReqMsg req = RankProto.CrossGroupRankTopByTypeReqMsg.parseFrom(packet.getBytes());
        groupId = req.getGroupId();
        typeList = req.getTypeList();
        bigType = req.getBigType();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        Map<Integer, Map<Integer, Map<Integer, CrossGroupRankListModel>>> modelMap = CrossRankMgr.getAllCrossGroupRankListModelMap();
        Map<Integer, Map<Integer, CrossGroupRankListModel>> rankMap = modelMap.get(groupId);
        Map<Integer, CrossGroupRankListModel> rankListModelMap = rankMap.get(bigType);
        RankProto.GetRankTopByTypeRespMsg.Builder builder = RankProto.GetRankTopByTypeRespMsg.newBuilder();
        for (Integer type : typeList) {
            CrossGroupRankListModel model = rankListModelMap.get(type);
            if (model != null) {
                RankProto.RankListMsg.Builder rankListMsg = model.getRankListMsg();
                if (rankListMsg.getRankListCount() > 0) {
                    builder.addTop(RankPb.getTopRankTemp(type, rankListMsg.getRankList(0)));
                }
            }
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_BIG_RANK_TYPE_TOP, builder);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
